package com.cn.sl.server.api.system.role.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import com.cn.sl.server.api.system.role.model.dto.AddRoleParam
import com.cn.sl.server.api.system.role.model.dto.EditRoleParam
import com.cn.sl.server.api.system.role.model.dto.RoleDto
import com.cn.sl.server.api.system.role.model.dto.RoleListQueryDto
import com.cn.sl.server.api.system.role.service.RoleService
import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.common.dto.PageDto
import com.cn.sl.server.util.ResponseMessageDto
import com.cn.sl.server.util.deleteSuccessMsg
import com.cn.sl.server.util.ok
import com.cn.sl.server.util.saveSuccessMsg
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 角色管理
 *
 * @author 阮雪峰
 */
@Tag(name = "角色管理")
@SaCheckLogin
@RestController
@RequestMapping("${Constant.ADMIN_ROOT}/role")
class RoleController(
    val roleService: RoleService
) {

    /**
     * 列表查询所有
     *
     * @param dto dto
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "列表查询所有")
    @PostMapping("/find-all")
    fun findAll(@RequestBody dto: RoleListQueryDto): ResponseEntity<List<RoleDto>> {
        return ok(roleService.findAll(dto))
    }

    /**
     * 列表查询
     *
     * @param dto dto
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "列表查询")
    @PostMapping("/find")
    fun find(@RequestBody dto: RoleListQueryDto): ResponseEntity<PageDto<RoleDto>> {
        return ok(roleService.find(dto))
    }

    /**
     * 新增
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    fun add(@RequestBody @Validated param: AddRoleParam): ResponseEntity<ResponseMessageDto> {
        roleService.add(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 编辑
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "编辑")
    @PostMapping("/edit")
    fun edit(@RequestBody @Validated param: EditRoleParam): ResponseEntity<ResponseMessageDto> {
        roleService.edit(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id查询")
    @GetMapping("/find/{id}")
    fun findById(@PathVariable id: Long): ResponseEntity<RoleDto> {
        return ok(roleService.findById(id))
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id删除")
    @DeleteMapping("/delete/{id}")
    fun delete(@PathVariable id: Long): ResponseEntity<ResponseMessageDto> {
        roleService.delete(id)
        return deleteSuccessMsg()
    }
}